package com.xbongbong.paas.model.impl;

import com.alibaba.fastjson.JSON;
import com.xbongbong.sys.constant.PaasFormTableNameConstant;
import com.xbongbong.paas.domain.dao.PaasFormExplainDao;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormExplainEntityExt;
import com.xbongbong.paas.help.BaseExplainModelHelp;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.sys.help.SysEnvHelp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author: wufeng
 * @date: 2018/8/13 15:22
 * @desrcption: PaaS主表单字段实现类
 */
@Service("paasFormExplainModel")
public class PaasFormExplainModelImpl extends BaseExplainModelHelp implements PaasFormExplainModel {

    /**
     * 检索菜单列表
     */
    private static final String COLUMNS = " id, corpid, app_id, menu_id, form_id, saas_mark, business_type, explains, enable, creator_id, add_time, update_time, del, distributor_mark";

    @Resource
    private PaasFormExplainDao paasFormExplainDao;
    @Resource
    private SysEnvHelp sysEnvHelp;

    @Override
    public Integer insert(PaasFormExplainEntity entity) {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return paasFormExplainDao.insert(entity);
    }

    @Override
    public List<PaasFormExplainEntity> insertBatch(List<PaasFormExplainEntity> formExplainList) {
        paasFormExplainDao.insertBatch(formExplainList);
        return formExplainList;
    }

    @Override
    public List<PaasFormExplainEntity> list(Map<String, Object> param) throws XbbException{
        param.put("columns", COLUMNS);
        List<PaasFormExplainEntity> explainList = paasFormExplainDao.findEntitys(param, PaasFormTableNameConstant.TB_PAAS_FORM_EXPLAIN);
        if (param.containsKey(StringConstant.CORPID)) {
            handleExplain(explainList, param.get(StringConstant.CORPID).toString());
        }
        return explainList;
    }

    @Override
    public List<PaasFormExplainEntity> findEntitys(Map<String, Object> entity) throws XbbException {
        List<PaasFormExplainEntity> explainList = paasFormExplainDao.findEntitys(entity, PaasFormTableNameConstant.TB_PAAS_FORM_EXPLAIN);
        if (entity.containsKey(StringConstant.CORPID) && Objects.nonNull(entity.get(StringConstant.CORPID))) {
            handleExplain(explainList, entity.get(StringConstant.CORPID).toString());
        }
        return explainList;
    }

    @Override
    public List<PaasFormExplainEntity> getNoHandleExplainsByFindEntitys(Map<String, Object> entity) {
       return paasFormExplainDao.findEntitys(entity, PaasFormTableNameConstant.TB_PAAS_FORM_EXPLAIN);
    }


    @Override
    public List<PaasFormExplainEntityExt> findEntityExt(Map<String, Object> param) throws XbbException{
        param.put("columns", COLUMNS);
        List<PaasFormExplainEntity> explainList = paasFormExplainDao.findEntitys(param, PaasFormTableNameConstant.TB_PAAS_FORM_EXPLAIN);
        if (param.containsKey(StringConstant.CORPID)) {
            handleExplain(explainList, param.get(StringConstant.CORPID).toString());
        }
        List<PaasFormExplainEntityExt> resultList = new ArrayList<>();
        explainList.forEach(item->{
            PaasFormExplainEntityExt paasFormExplainEntityExt = new PaasFormExplainEntityExt(item.getId(), item.getCorpid(), item.getAppId(), item.getMenuId(), item.getFormId(),
                    item.getEnable(), item.getExplains(), item.getCreatorId(), item.getAddTime(), item.getUpdateTime(), item.getDel(), item.getSaasMark(), item.getBusinessType(), item.getDistributorMark(),
                    JSON.parseArray(item.getExplains()) );
            resultList.add(paasFormExplainEntityExt);
        });
        return resultList;
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> map) {
        return paasFormExplainDao.getEntitysCount(map);
    }

    @Override
    public Integer delete(Long key, String corpid) {
        return  paasFormExplainDao.deleteByKey(key, corpid);
    }

    @Override
    public Integer update(PaasFormExplainEntity entity) {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        return paasFormExplainDao.update(entity);
    }

    @Override
    public PaasFormExplainEntity getByKey(Integer key, String corpid) throws XbbException{
        PaasFormExplainEntity paasFormExplainEntity = paasFormExplainDao.getByKey(key, corpid);
        handleExplainSingle(paasFormExplainEntity, corpid);
        return paasFormExplainEntity;
    }

    @Override
    public PaasFormExplainEntity getByMenuId(Long menuId, String corpid) throws XbbException{
        PaasFormExplainEntity paasFormExplainEntity = paasFormExplainDao.getByMenuId(menuId, corpid);
        handleExplainSingle(paasFormExplainEntity, corpid);
        return paasFormExplainEntity;
    }

    @Override
    public PaasFormExplainEntity getByMenuIdAndBusinessType(Integer menuId, String corpid, Integer businessType)throws XbbException{
        PaasFormExplainEntity paasFormExplainEntity = paasFormExplainDao.getByMenuIdAndBusinessType(menuId, corpid, businessType);
        handleExplainSingle(paasFormExplainEntity, corpid);
        return paasFormExplainEntity;
    }

    @Override
    public PaasFormExplainEntity getByFormId(Long formId, String corpid) throws XbbException{
        PaasFormExplainEntity paasFormExplainEntity = paasFormExplainDao.getByFormId(formId, corpid);
        handleExplainSingle(paasFormExplainEntity, corpid);
        return paasFormExplainEntity;
    }

    @Override
    public Integer updateByFormId(Long formId, String corpid, Integer enable) {
        return paasFormExplainDao.updateByFormId(formId, corpid, enable);
    }

    @Override
    public PaasFormExplainEntity getByBusinessType(Integer businessType, String corpid) throws XbbException{
        PaasFormExplainEntity paasFormExplainEntity = paasFormExplainDao.getByBusinessType(businessType,corpid, PaasFormTableNameConstant.TB_PAAS_FORM_EXPLAIN);
        handleExplainSingle(paasFormExplainEntity, corpid);
        return paasFormExplainEntity;
    }

    @Override
    public PaasFormExplainEntity getByBusinessType4Distributor(Integer businessType, String corpid, Integer distributor) throws XbbException{
        if (Objects.isNull(distributor)) {
            distributor = DistributorMarkEnum.OTHER.getCode();
        }
        PaasFormExplainEntity paasFormExplainEntity = paasFormExplainDao.getByBusinessType4Distributor(businessType,corpid, distributor, PaasFormTableNameConstant.TB_PAAS_FORM_EXPLAIN);
        handleExplainSingle(paasFormExplainEntity, corpid);
        return paasFormExplainEntity;
    }

    @Override
    public PaasFormExplainEntity getByBusinessTypeNoEnable(Integer businessType, String corpid) throws XbbException {
        PaasFormExplainEntity paasFormExplainEntity = paasFormExplainDao.getByBusinessTypeNoEnable(businessType,corpid);
        handleExplainSingle(paasFormExplainEntity, corpid);
        return paasFormExplainEntity;
    }

    @Override
    public PaasFormExplainEntity getSystemDefaultExplain(Integer businessType) throws XbbException{
        PaasFormExplainEntity paasFormExplainEntity;
        if (sysEnvHelp.isGrayEnvironment()) {
            // 灰度
            paasFormExplainEntity = paasFormExplainDao.getSystemDefaultExplain(businessType, PaasFormTableNameConstant.TB_PAAS_FORM_EXPLAIN_GRAY);
        } else {
            // 正式
            paasFormExplainEntity = paasFormExplainDao.getSystemDefaultExplain(businessType, PaasFormTableNameConstant.TB_PAAS_FORM_EXPLAIN);
        }
        return paasFormExplainEntity;
    }

    @Override
    public List<String> getCorpidList() {
        return paasFormExplainDao.getCorpidList();
    }

    @Override
    public Integer updateBatchExplains(List<PaasFormExplainEntity> list) {
        return paasFormExplainDao.updateBatchExplains(list);
    }

    @Override
    public Integer updateBatchExplains(List<PaasFormExplainEntity> list, String corpid) {
        return paasFormExplainDao.updateBatchExplainsByCorpid(list, corpid);
    }

    @Override
    public List<PaasFormExplainEntity> getListForUpdateProduct(String corpid, List<Integer> businessTypeList) throws XbbException {
        List<PaasFormExplainEntity> explainList = paasFormExplainDao.getListForUpdateProduct(corpid,businessTypeList);
        handleExplain(explainList, corpid);
        return explainList;
    }

    @Override
    public PaasFormExplainEntity getOriStockForm() {
        return paasFormExplainDao.getOriStockForm();
    }

    @Override
    public void updateBatch(List<PaasFormExplainEntity> list, String corpid) {
        paasFormExplainDao.updateBatch(list, corpid);
    }
    /**
     * 批量删除
     * @param list 主键id集合
     * @param corpid
     * @author xingxing.xiao
     */
    @Override
    public Integer deleteBatch(List<Long> list, String corpid) {
        return paasFormExplainDao.deleteBatch(list, corpid);
    }

    @Override
    public PaasFormExplainEntity getWorkReportDefaultAllListExplain() throws XbbException {
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", "0");
        param.put("del", 0);
        param.put("businessType", XbbRefTypeEnum.WORK_REPORT.getCode());
        List<PaasFormExplainEntity> entitys = list(param);
        return entitys.get(0);
    }

    @Override
    public Integer deleteBatchByMenuId(List<Long> list, String corpid) {
        return paasFormExplainDao.deleteBatchByMenuId(list, corpid);
    }

    @Override
    public Long getMaxId(Map<String, Object> map) {
        return paasFormExplainDao.getMaxId(map);
    }

    @Override
    public List<PaasFormExplainEntity> getByFormIdIn(Collection<Long> formIdIn, String corpid) throws XbbException {
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", corpid);
        param.put("formIdIn", formIdIn);
        param.put("del", 0);
        List<PaasFormExplainEntity> list = findEntitys(param);
        handleExplain(list, corpid);
        return list;
    }
}
